﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReAnimation.MVP.ViewInterfaces;
using ReAnimation.MVP.Models.Interfaces;
using ReAnimation.MVP.Presenters;
using ReAnimation.MVP.Presenters.Helpers;
using ReAnimation.Helpers.Common;
using System.Collections;
using ReAnimation.Helpers.WinForms;
using ReAnimation.Domain;

namespace ReAnimation.MVP.Views
{
    public partial class AnimationStepsView : BaseUserControlView, IAnimationStepsView
    {
        private bool internalListBoxChange = false;
        IMainModel _mainModel;
        AnimationStepsPresenter _presenter;

        public AnimationStepsView(IMainModel mainModel,
                                  AnimationStepsPresenter presenter)
        {
            _mainModel = mainModel;
            _presenter = presenter;
            
            InitializeComponent();

            _presenter.View = this;

            _mainModel.CurrentAnimationStepChanged += (s, a) => { SyncViewWithModel(); };
            _mainModel.CurrentAnimationStepPercentChanged += (s, a) => { SyncViewWithModel(); };
            SyncViewWithModel();
        }

        #region IAnimationStepsView Members

        public event EventHandler Add;

        public event EventHandler Remove;

        public event EventHandler Apply;

        public event EventHandler<EventArg<AnimationStep>> AnimationStepSelect;

        public string Percent
        {
            get
            {
                return tbxAnimationStepTime.Text;
            }
        }

        #endregion

        private void SyncViewWithModel()
        {
            using (new BatchLayoutChange(this))
            {
                if (_mainModel.CurrentAnimation == null)
                {
                    lbxAnimationSteps.Items.Clear();
                    tbxAnimationStepTime.Text = String.Empty;
                }
                else
                {
                    //TODO: shit
                    internalListBoxChange = true;
                    lbxAnimationSteps.DataSource = PrepareAnimationStepsItemsForListBox();
                    lbxAnimationSteps.DisplayMember = "DisplayName";//FIXME: WTF
                    lbxAnimationSteps.ValueMember = "Value";
                    internalListBoxChange = false;

                    if (_mainModel.CurrentAnimationStep == null)
                        tbxAnimationStepTime.Text = String.Empty;
                    else
                    {
                        lbxAnimationSteps.SelectedValue = _mainModel.CurrentAnimationStep;

                        tbxAnimationStepTime.Text = GetCurrentAnimationStepPercentString();
                    }
                }
            }
        }

        private string GetCurrentAnimationStepPercentString()
        {
            return "{0:00.0}".F(_mainModel.CurrentAnimationStep.Percent).Replace(",", ".");
        }

        private IEnumerable<object> PrepareAnimationStepsItemsForListBox()
        {
            if (_mainModel.CurrentAnimation == null ||
               _mainModel.CurrentAnimation.AnimationSteps == null)
                return new List<object>();

            return (from obj in _mainModel.CurrentAnimation.AnimationSteps orderby obj.Percent
                    select new
                    {
                        DisplayName = obj.Percent.ToString(),
                        Value = obj
                    }).Cast<object>().ToList();
        }

        private void btnAddAnimationStep_Click(object sender, EventArgs e)
        {
            Add.Throw(sender, e);
        }

        private void lbxAnimationSteps_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (internalListBoxChange) return;//TODO: ???
            var selectedValue = lbxAnimationSteps.SelectedValue;
            AnimationStepSelect.Throw(sender, new EventArg<AnimationStep>(selectedValue as AnimationStep));
        }

        private void btnRemoveAnimationStep_Click(object sender, EventArgs e)
        {
            Remove.Throw(sender, e);
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            Apply.Throw(sender, e);
        }

        private void lblAnimationStep_Click(object sender, EventArgs e)
        {

        }
    }
}
